package cc.ufinity.networkdiagnose.adapter;
 
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface; 
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button; 
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.app.GlobalApp; 
import cc.ufinity.networkdiagnose.view.R;

public class TaskTestListAdapter extends BaseAdapter {

	private Activity mContext;
	private List<Map<String, Object>> datas;
	private View view;
	private LayoutInflater li;
	
	public TaskTestListAdapter(Activity context, List<Map<String, Object>> datas)
	{
		mContext = context;
		this.datas = datas;
	}
	
	@Override
	public int getCount() {
		
		if(datas == null) {
			return 0;
		}
		return datas.size();
	}
	
	@Override
	public Object getItem(int position) {
		try {
			if(datas == null)
				return null;
			else
				return datas.get(position);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null)
		{
			li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = (LinearLayout) li.inflate(R.layout.task_test_list_widget, null);
		} else {
			view = convertView;
		}
		
		FillDataIntoView(view,position);
		
		return view;
	}
	
	public void FillDataIntoView(View view, int position)
	{
		TextView tv, tv_rate;	
		final ImageView iv;
		Button bt;
		ProgressBar pb;
		LinearLayout ll;

		final Map<String, Object> item = datas.get(position);
	//	System.out.println(position + " = " + item.toString());
		final String title = (String)item.get("taskName");
		
		
		bt = (Button)view.findViewById(R.id.task_bt_skip);
		ll = (LinearLayout)view.findViewById(R.id.task_progress_layout);
		pb = (ProgressBar)view.findViewById(R.id.task_progress);
		tv_rate = (TextView)view.findViewById(R.id.task_progress_value);
		ll.setVisibility(LinearLayout.INVISIBLE);
		tv = (TextView)view.findViewById(R.id.task_name);
		tv.setText(title);
		
		iv = (ImageView)view.findViewById(R.id.task_test_state);
		String state = (String)item.get("taskState");
		String skip = (String)item.get("taskSkip");
		if(state.equals("1")) { //已检测
			iv.setBackgroundResource(R.drawable.test_pass); 
			bt.setText("查看");
		} else if(state.equals("0")) { //待检测或以跳过
			if(skip.equals("1")) {
				iv.setBackgroundResource(R.drawable.test_error);
				bt.setText("已跳过");
				bt.setClickable(false);
			} else if(skip.equals("0")) {
				iv.setBackgroundResource(R.drawable.test_wait);
				bt.setText("跳过");
			}
		} else if(state.equals("3")) { //正在检测
			iv.setBackgroundResource(R.drawable.test_ing);
			bt.setText("检测中");
			if(title.equals("FTP测试")){
				try {
					int progress = GlobalApp.ftpTestProxyWidget.getTestedTimes();
					int max = GlobalApp.ftpTestProxyWidget.getTestCount();
					pb.setMax(max);
					pb.setProgress(progress);
					tv_rate.setText("已完成：" + progress*100/max + "%");
					ll.setVisibility(LinearLayout.VISIBLE);
				} catch(Exception e) {
					ll.setVisibility(LinearLayout.INVISIBLE);
				}
			}
		}
		
		bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(((Button)v).getText().equals("查看")) {
					/*StringBuilder sb = new StringBuilder();
					sb.append("taskName: " + item.get("taskName")).append("\n");
					sb.append("taskState: " + item.get("taskState")).append("\n");
		            showTaskResultDialog(sb.toString());*/
					/**
					 * tk0.taskName = "HTTP测试";  
						tk1.taskName = "FTP测试";
						tk2.taskName = "PING测试";  
						tk3.taskName = "AP关联";
						tk4.taskName = "WEB认证";  
						tk5.taskName = "隔离测试";
						tk6.taskName = "漫游测试";  
						tk7.taskName = "信噪比测试";
						tk8.taskName = "信号强度SRRI";  
						tk9.taskName = "PEAP认证";
						tk.taskName = "SIM认证";
					 */
					if(title.equals("HTTP测试")) {
						showTaskResultDialog(GlobalApp.result_HTTP_test.toString(), title);
						
					} else if(title.equals("FTP测试")) {
						showTaskResultDialog(GlobalApp.result_FTP_test.toString(), title);
						
					} else if(title.equals("PING测试")) {
						showTaskResultDialog(GlobalApp.result_PING_test.toString(), title);
						
					} else if(title.equals("AP关联")) {  
						showTaskResultDialog(GlobalApp.result_AP_test.toString(), title);
						
					} else if(title.equals("WEB认证")) {
						showTaskResultDialog(GlobalApp.result_WEB_test.toString(), title);
						
					} else if(title.equals("漫游测试")) {
						showTaskResultDialog(GlobalApp.result_ROAM_test.toString(), title);
						
					} else if(title.equals("隔离测试")) {
						showTaskResultDialog(GlobalApp.result_SEGREGATE_test.toString(), title);
						
					} else if(title.equals("信噪比测试")) {
						showTaskResultDialog(GlobalApp.result_SNR_test.toString(), title);
						
					} else if(title.equals("信号强度RSSI")) {
						showTaskResultDialog(GlobalApp.result_SRRI_test.toString(), title);
						
					} else if(title.equals("PEAP认证")) {
						showTaskResultDialog(GlobalApp.result_PEAP_test.toString(), title);
						
					} else if(title.equals("SIM认证")) {
						showTaskResultDialog(GlobalApp.result_SIM_test.toString(), title);
						
					}
		            
				} else if(((Button)v).getText().equals("跳过")) {
					item.put("taskSkip", "1");
					((Button)v).setText("已跳过");
					((Button)v).setClickable(false);
					iv.setBackgroundResource(R.drawable.test_error);
					System.out.println("跳过按钮：" + item.toString());
				}
				
			}
		});
		
	}
	
	private void showTaskResultDialog(String result, String title) {
		LayoutInflater factory = LayoutInflater.from(mContext);
		final View dialogView = factory.inflate(
				R.layout.show_task_result, null);
		final TextView text = (TextView) dialogView
				.findViewById(R.id.task_result);
		text.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置为可以滚动
		text.setText(result);
		AlertDialog.Builder inputBuilder = new AlertDialog.Builder(mContext);
		inputBuilder.setTitle(title + "结果");
//		inputBuilder.setIcon(R.drawable.smile);
		inputBuilder.setView(dialogView);
	//	inputBuilder.setMessage(result);
		inputBuilder.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
				            
							dialog.cancel();
					}
				});
		Dialog dialog = inputBuilder.create();
		dialog.show();
	}

}
